<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    
    <div class="table-page-search-wrapper">
      <a-form layout="inline" :form="form" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">          
          <a-col :md="6" :sm="8">
            <a-form-item label="展会主题">
              <a-select v-model="exhibitionMeetingId">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option v-for="(item, index) in dictOptiond" :key="index" :value="item.id">
                  <span style="display: inline-block;width: 100%">
                    {{ item.theme }}
                  </span>
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="分销商">
              <a-select v-model="distributorId">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option v-for="(item, index) in dictOption" :key="index" :value="item.id">
                  <span style="display: inline-block;width: 100%">
                    {{ item.name }}
                  </span>
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!--<a-col :md="6" :sm="8">
            <a-form-item label="分销商手机号">
              <a-input placeholder="请输入手机号" v-model="queryParamphone"></a-input>
            </a-form-item>
          </a-col> -->
          <a-col :md="6" :sm="8" >
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" v-if="userAuth2btn == '3' || userAuth2btn == '4'" type="primary">门票分销</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('展会门票管理')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <!-- <a-button type="primary" v-if="dataSource.length>0" icon="download" @click="handleExportXls('二维码')">下载二维码</a-button> -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>
    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{fixed:true,selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
          <div v-else @click="jumpProview(text)">
            <img :src="getImgView(text)" height="25px" alt="图片不存在" style="max-width:80px;font-size: 12px;font-style: italic;"/>
          </div>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)"
          >下载</a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-popconfirm  v-if="record.bindStatus=='1'" title="确定解除绑定吗?" @confirm="() => handleUnbind(record.id)">
            <a>解除绑定</a>
          </a-popconfirm>
          
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多
              <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>
    <JProviewImg v-if="showImg" @closeProview="closeProview" :imgurl="proviewUrl"></JProviewImg>
    <exhibition_tickets-modal ref="modalForm" @ok="modalFormOk"></exhibition_tickets-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import Exhibition_ticketsModal from './modules/Exhibition_ticketsModal'
// import BlockProductBindInfoModal from './modules/BlockProductBindInfoModal'
import { initDictOptions, filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { getAction, putAction, postAction,downFile } from '@/api/manage'
import JProviewImg from '@/components/jeecg/JProviewImg'
import { ACCESS_TOKEN } from "@/store/mutation-types"
import Vue from 'vue'

export default {
  name: 'Exhibition_product_bind_info_newList',
  mixins: [JeecgListMixin],
  components: {
    //   BlockProductBindInfoModal,
      JProviewImg,
      Exhibition_ticketsModal
  },
  data() {
    return {
      dictOption:[],
      dictOptiond:[],
      form: this.$form.createForm(this),
      exhibitionMeetingId:"",
      distributorId:"",
      userAuth2btn:"1",
      ipagination:{
        current: 1,
        pageSize: 10,
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
        },
        showQuickJumper: true,
        total: 0
      },
      manufacturerName:"",
      headers:{},
      proviewUrl:"",
      showImg:false,
      specification:[],
      description: '剧本绑定信息管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        // {
        //   title: '发行方名称',
        //   align: 'center',
        //   dataIndex: 'manufacturerName'
        // },
        {
          title: '门票码',
          align:"center",
          dataIndex: 'captcha'
        },   
        {
          title: '二维码',
          align: 'center',
          dataIndex: 'ticketsImagePath',
          scopedSlots: { customRender: 'imgSlot' }
        },
         {
            title: '票具主次类型',
            align:"center",
            dataIndex: 'ticketsType',
            customRender: text => {
              return text==1?'主票':"次票"
            }
        },
        {
          title: '店家手机号',
          align:"center",
          dataIndex: 'phonenumber'
        },         
        {
          title: '店家地址',
          align:"center",
          dataIndex: 'shopaddr'
        },
        {
          title: '分销商',
          align:"center",
          dataIndex: 'distriteName'
        },
        {
          title: '分销商手机号',
          align:"center",
          dataIndex: 'distritePhone'
        },
        {
          title: '绑定状态',
          align:"center",
          dataIndex: 'bindStatus',
          customRender: text => {
            return text==0?'未绑定':"已绑定"
          }
        },
        {
          title: '绑定时间',
          align:"center",
          dataIndex: 'bindTime'
        },
        // {
        //   title: '操作',
        //   dataIndex: 'action',
        //   align: 'center',
        //   scopedSlots: { customRender: 'action' }
        // }
      ],
      url: {
        list: '/exhibition/exhibition_meeting_tickets/list',
        delete: '/exhibition/exhibition_meeting_tickets/delete',
        deleteBatch: '/exhibition/exhibition_meeting_tickets/deleteBatch',
        exportXlsUrl: '/exhibition/exhibition_meeting_tickets/download',
        importExcelUrl: '/exhibition/exhibition_meeting_tickets/importExcel',
        unbindData: '/exhibition/exhibition_meeting_tickets/unBindData'
      },
      dictOptions: {
        bindStatus: []
      },
      productId:"",
      productNameVal:""
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  mounted(){
    var paramd={
        column:"createTime",
        order:"desc",
        field:"id,,,name,phone,remark,action",
        pageNo:1,
        pageSize: 1000
      };
      getAction("/exhibition/exhibition_distributor_info/list",paramd).then((res) => {
        if (res.success) {
          this.dictOption = res.result.records;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    getAction("/block/blockManufacturerInfo/userAuth2btn")
        .then(res => {
          if (res.success) {
            this.userAuth2btn = res.result.status;
          } else {
            that.$message.warning(res.message)
          }
        }) 
        var params={
        column:"createTime",
        order:"desc",
        field:"id,,,name,phone,remark,action",
        pageNo:1,
        pageSize: 1000
      };
      getAction("/exhibition/exhibition_meeting_info/list",params).then((res) => {
        if (res.success) {
          this.dictOptiond = res.result.records;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    this.indataList();
  },
  methods: {
    handleExportXls(fileName){
      if(!this.exhibitionMeetingId){
        this.$message.warning("请选择展会主题！")
        return
      }    
      this.$message.warning("下载时间可能需要1-2分钟请耐心等待！")    
      let param = {
        "exhibition_meeting_id":this.exhibitionMeetingId
      }
      getAction("/exhibition/exhibition_meeting_tickets/predownloadTickets",param).then((data)=>{
        console.log(data)
        if (!data.message) {
          this.$message.warning("文件下载失败")
          return
        }
        var paramList = {
          "exhibition_meeting_id":this.exhibitionMeetingId,
          "zipFilePath":data.message
        }
        downFile("/exhibition/exhibition_meeting_tickets/downloadTickets",paramList).then((res) => {
          // if (res.success) {
          //   this.$message.warning(res.message)
          // }
          // if(res.code===510){
          //   this.$message.warning(res.message)
          // }
          // downFile("/exhibition/exhibition_tickets/downloadTickets",param).then((data)=>{})
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([res]), fileName+'.zip')
          }else{
            let url = window.URL.createObjectURL(new Blob([res]))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName+'.zip')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
          }
        })
        
        // if (typeof window.navigator.msSaveBlob !== 'undefined') {
        //   window.navigator.msSaveBlob(new Blob([data]), fileName+'.zip')
        // }else{
        //   let url = window.URL.createObjectURL(new Blob([data]))
        //   let link = document.createElement('a')
        //   link.style.display = 'none'
        //   link.href = url
        //   link.setAttribute('download', fileName+'.zip')
        //   document.body.appendChild(link)
        //   link.click()
        //   document.body.removeChild(link); //下载完成移除元素
        //   window.URL.revokeObjectURL(url); //释放掉blob对象
        // }
      })
    },
    modalFormOk() {
      var paramd={
        exhibitionMeetingId:this.exhibitionMeetingId,
        distributorId:this.distributorId,
        column:"createTime",
        order:"desc",
        field:"id,,,name,phone,remark,action",
        pageNo:1,
        pageSize: 10
      };
      getAction("/exhibition/exhibition_tickets/list",paramd).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    },
    searchQuery(){
      this.ipagination.current = 1;
      var params={
        exhibitionMeetingId:this.exhibitionMeetingId,
        distributorId:this.distributorId,
        column:"createTime",
        order: "asc",
        field: "id,,,manufacturerName,productName,qrcodeImage",
        pageNo: this.ipagination.current,
        pageSize: 10
      };
      getAction("/exhibition/exhibition_meeting_tickets/list",params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    },
    handleTableChange(pagination, filters, sorter){
      this.ipagination = pagination;
      var params={
        exhibitionMeetingId:this.exhibitionMeetingId,
        distributorId:this.distributorId,
        column:"createTime",
        order: "asc",
        field: "id,,,manufacturerName,productName,qrcodeImage",
        pageNo: pagination.current,
        pageSize: 10
      };
      getAction("/exhibition/exhibition_meeting_tickets/list",params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    },
    changeSpecification(value,option){
      this.productId = option.key;
      this.productNameVal = value;
      var params={
        productName: value,
        column:"createTime",
        order: "asc",
        field: "id,,,manufacturerName,productName,qrcodeImage",
        pageNo: 1,
        pageSize: 10
      };
      getAction("/exhibition/exhibition_product_bind_info_new/list",params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    },
    indataList(){
      var params={
        productStandardsId: "1216548480059949057",
        order: "asc",
        field: "",
        pageNo: 1,
        pageSize: 1000
      };
      getAction("/exhibition/exhibition_product_standards_new/list",params).then((res) => {
        if (res.success) {
          this.specification = res.result.records;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    },
    closeProview(){
      this.showImg = false;
    },
    jumpProview(val){
      this.showImg = true;
      this.proviewUrl = val;
    },
    initDictConfig() {
      initDictOptions('bind_status').then(res => {
        if (res.success) {
          this.$set(this.dictOptions, 'bindStatus', res.result)
        }
      })
    },
    handleUnbind: function(id) {
      var params = {
        id: id
      }
      getAction(this.url.unbindData, params).then(res => {
        if (res.success) {
          
          this.$message.warning(res.message);
          this.loadData();
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
      })
    }
  },
  created () {
    const token = Vue.ls.get(ACCESS_TOKEN);
    this.headers = {"X-Access-Token":token}
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>